﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.Locomotive.TrainRepair.Work.WorkPub
{
    public partial class Locomotive_ElectricRepair_Work_WorkPub_Day_Pub : DevExpress.XtraEditors.XtraForm
    {
        public string _str_Font_Size = "0";
        public string _LoadWorkType = "2";
        public Locomotive_ElectricRepair_Work_WorkPub_Day_Pub()
        {
            InitializeComponent();
        }

        private void Locomotive_ElectricRepair_Work_WorkPub_Day_Pub_Load(object sender, EventArgs e)
        {
            InitTrainInList();
        }

        private void Locomotive_ElectricRepair_Work_WorkPub_Day_Pub_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        #region//--在修机车列表事件--//
        /// <summary>
        /// 载入在修机车列表
        /// </summary>
        public void InitTrainInList()
        {
            flp_Main.Controls.Clear();
            Panel _pl = new Panel();
            Panel _pl_s = new Panel();
            string strSQL = "select f_id,f_ttname+' '+(select f_name from TS_S_T_P_Train_RP where f_id = TS_TR_BU_Work_Main.f_rpid) as f_ttname,f_name ,convert(nvarchar(10),f_stime,23)as f_stime from TS_TR_BU_Work_Main where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_type=" + _LoadWorkType;
            string _str = "";
            if (rb_TrainNumber.Checked)
            {
                TextBox _tb = (TextBox)flp_Query.Controls.Find("tb_Name", true)[0];
                if (_tb.Text.Trim().Length > 0)
                {
                    strSQL += " and f_name like '%" + _tb.Text.Trim() + "%'";
                }
            }
            else
            {
                CheckBox _cb = new CheckBox();
                foreach (Control _cl in flp_Query.Controls)
                {
                    _cb = (CheckBox)_cl;
                    if (_cb.Checked)
                    {
                        _str += _cb.Tag.ToString() + ",";
                    }
                }
                if (_str.Length > 0)
                {
                    _str = _str.Substring(0, _str.Length - 1);
                    if (rb_WorkPlace.Checked)
                    {
                        string[] _Arry = _str.Split(',');
                        string _strQueryList = "";
                        for (int ii = 0; ii < _Arry.Length; ii++)
                        {
                            _strQueryList += "f_level like '%," + _Arry[ii] + ",%' or ";
                        }
                        _strQueryList = _strQueryList.Substring(0, _strQueryList.Length - 4);
                        strSQL += "and f_tpmapid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 and f_type=0 and f_level<>',0,63,' and (" + _strQueryList + "))";
                    }
                    if (rb_TrainType.Checked)
                    {
                        strSQL += " and f_ttid in (" + _str + ")";
                    }
                }
            }
            strSQL += " and f_del=0 order by f_ttname,f_name asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            SqlConnection conn_s = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn_s.Open();
            SqlCommand comm_s = new SqlCommand("select getdate()", conn_s);
            SqlDataReader read_s = comm_s.ExecuteReader();
            int i = 0;
            while (read.Read())
            {
                _pl = new Panel();
                //_pl.AutoScroll = true;
                if (_str_Font_Size == "0")
                {
                    _pl.Size = new System.Drawing.Size(flp_Main.ClientSize.Width, 60);
                    _pl.BorderStyle = BorderStyle.FixedSingle;
                    _pl.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                }
                else
                {
                    _pl.Size = new System.Drawing.Size(flp_Main.ClientSize.Width, 120);
                    _pl.BorderStyle = BorderStyle.FixedSingle;
                    _pl.Font = new System.Drawing.Font("微软雅黑", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                }
                if (i == 0)
                {
                    _pl.BackColor = Color.LightGray;
                }
                else
                {
                    _pl.BackColor = Color.LightCyan;
                }
                _pl.Name = "_pl_" + read["f_id"].ToString();
                Button _bt = new Button();
                _pl_s = new Panel();
                _pl_s.Tag = read["f_id"].ToString();
                _bt.Text = read["f_ttname"].ToString() + "\n" + read["f_name"].ToString() + "\n" + read["f_stime"].ToString();
                _bt.Tag = _pl_s;
                _pl.Tag = _pl_s;
                _pl.Controls.Add(_bt);
                _pl_s.AutoScroll = true;
                if (_str_Font_Size == "0")
                {
                    _bt.Size = new System.Drawing.Size(148, 58);
                    _bt.Location = new Point(0, 0);
                    _pl_s.Size = new System.Drawing.Size(flp_Main.ClientSize.Width - 150, 60);
                    _pl_s.BorderStyle = BorderStyle.FixedSingle;
                    _pl_s.Location = new Point(148, 0);
                }
                else
                {
                    _bt.Size = new System.Drawing.Size(296, 118);
                    _bt.Location = new Point(0, 0);
                    _pl_s.Size = new System.Drawing.Size(flp_Main.ClientSize.Width - 300, 120);
                    _pl_s.BorderStyle = BorderStyle.FixedSingle;
                    _pl_s.Location = new Point(296, 0);
                }
                _bt.Click += new EventHandler(Button_Train_Click);
                strSQL = "select f_id,f_name,f_state,CONVERT(varchar(100), f_pstime, 23) as f_stime,(select f_string from TS_S_T_S_ColorList where f_id=(select f_cid from TS_S_T_S_RP_DataState where f_id=TS_TR_BU_Work_Main_Day.f_state)) as f_color from TS_TR_BU_Work_Main_Day where f_bmwid=" + read["f_id"].ToString() + " and f_del=0";
                if (rb_Steps_All.Checked)
                {
                }
                if (rb_Steps_W.Checked)
                {
                    strSQL += " and f_state<1";
                }
                if (rb_Steps_H.Checked)
                {
                    strSQL += " and f_state>0";
                }
                strSQL += " order by f_order asc";
                read_s.Close();
                comm_s = new SqlCommand(strSQL, conn_s);
                read_s = comm_s.ExecuteReader();
                int n = 0;
                while (read_s.Read())
                {
                    _bt = new Button();
                    _bt.Text = read_s["f_name"].ToString() + "\n" + read_s["f_stime"].ToString();
                    if (_str_Font_Size == "0")
                    {
                        _bt.Size = new System.Drawing.Size(148, 42);
                        _bt.Location = new Point(148 * n, 0);
                    }
                    else
                    {
                        _bt.Size = new System.Drawing.Size(296, 102);
                        _bt.Location = new Point(296 * n, 0);
                    }
                    _bt.BackColor = Color.FromName(read_s["f_color"].ToString());
                    _bt.Tag = read_s["f_id"].ToString();
                    _bt.Click += new EventHandler(Button_Click);
                    //                    _bt.MouseEnter += new EventHandler(Button_MouseEnter);
                    _pl_s.Controls.Add(_bt);
                    n++;
                }
                _pl.Controls.Add(_pl_s);
                flp_Main.Controls.Add(_pl);

                i++;
                if (i == 2)
                {
                    i = 0;
                }
            }
            read_s.Close();
            conn_s.Close();
            conn_s.Dispose();
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        private void _ReloadViewTrainList()
        {
            foreach (Control _cl in flp_Main.Controls)
            {
                Button _bt = new Button();
                Panel _pl_s = (Panel)_cl.Tag;
                SqlConnection conn_s = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn_s.Open();
                SqlCommand comm_s = new SqlCommand("select getdate()", conn_s);
                SqlDataReader read_s = comm_s.ExecuteReader();
                //_pl_s.Controls.Clear();
                string strSQL = "select f_id,f_name,f_state,CONVERT(varchar(100), f_pstime, 23) as f_stime,(select f_string from TS_S_T_S_ColorList where f_id=(select f_cid from TS_S_T_S_RP_DataState where f_id=TS_TR_BU_Work_Main_Day.f_state)) as f_color from TS_TR_BU_Work_Main_Day where f_bmwid=" + _pl_s.Tag.ToString() + " and f_del=0";
                if (rb_Steps_All.Checked)
                {
                }
                if (rb_Steps_W.Checked)
                {
                    strSQL += " and f_state<1";
                }
                if (rb_Steps_H.Checked)
                {
                    strSQL += " and f_state>0";
                }
                strSQL += " order by f_order asc";
                read_s.Close();
                comm_s = new SqlCommand(strSQL, conn_s);
                read_s = comm_s.ExecuteReader();
                while (read_s.Read())
                {
                    foreach (Control _cl_s in _pl_s.Controls)
                    {
                        _bt = (Button)_cl_s;
                        if (_bt.Tag.ToString() == read_s["f_id"].ToString())
                        {
                            _bt.BackColor = Color.FromName(read_s["f_color"].ToString());
                        }
                    }
                }
                read_s.Close();
                conn_s.Close();
                conn_s.Dispose();
            }
        }
        private void Button_Train_Click(object sender, EventArgs e)
        {
            Button _bt = new Button();
            _bt = sender as Button;
            Panel _pl_s = (Panel)_bt.Tag;
            SqlConnection conn_s = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn_s.Open();
            SqlCommand comm_s = new SqlCommand("select getdate()", conn_s);
            SqlDataReader read_s = comm_s.ExecuteReader();
            _pl_s.Controls.Clear();
            string strSQL = "select f_id,f_name,f_state,CONVERT(varchar(100), f_pstime, 23) as f_stime,(select f_string from TS_S_T_S_ColorList where f_id=(select f_cid from TS_S_T_S_RP_DataState where f_id=TS_TR_BU_Work_Main_Day.f_state)) as f_color from TS_TR_BU_Work_Main_Day where f_bmwid=" + _pl_s.Tag.ToString()+" and f_del=0";
            if (rb_Steps_All.Checked)
            {
            }
            if (rb_Steps_W.Checked)
            {
                strSQL += " and f_state<1";
            }
            if (rb_Steps_H.Checked)
            {
                strSQL += " and f_state>0";
            }
            strSQL += " order by f_order asc";
            read_s.Close();
            comm_s = new SqlCommand(strSQL, conn_s);
            read_s = comm_s.ExecuteReader();
            int n = 0;
            while (read_s.Read())
            {
                _bt = new Button();
                _bt.Text = read_s["f_name"].ToString() + "\n" + read_s["f_stime"].ToString();
                if (_str_Font_Size == "0")
                {
                    _bt.Size = new System.Drawing.Size(148, 42);
                    _bt.Location = new Point(148 * n, 0);
                }
                else
                {
                    _bt.Size = new System.Drawing.Size(296, 102);
                    _bt.Location = new Point(296 * n, 0);
                }
                _bt.BackColor = Color.FromName(read_s["f_color"].ToString());
                _bt.Tag = read_s["f_id"].ToString();
                _bt.Click += new EventHandler(Button_Click);
                _pl_s.Controls.Add(_bt);
                n++;
            }
            read_s.Close();
            conn_s.Close();
            conn_s.Dispose();
        }
        private void Button_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            string _BMWID = (sender as Button).Tag.ToString();
            UserCon.Locomotive.TrainRepair.Work.WorkPub.uc_Locomotive_ElectricRepair_Work_WorkPub_Day_Pub _uc = new UserCon.Locomotive.TrainRepair.Work.WorkPub.uc_Locomotive_ElectricRepair_Work_WorkPub_Day_Pub();
            _uc._BMWID = _BMWID;
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                Button _bt = (Button)sender;
                SqlConnection conn_s = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn_s.Open();
                SqlCommand comm_s = new SqlCommand("select getdate()", conn_s);
                SqlDataReader read_s = comm_s.ExecuteReader();
                string strSQL = "select f_id,f_name,f_state,CONVERT(varchar(100), f_pstime, 23) as f_stime,(select f_string from TS_S_T_S_ColorList where f_id=(select f_cid from TS_S_T_S_RP_DataState where f_id=TS_TR_BU_Work_Main_Day.f_state)) as f_color from TS_TR_BU_Work_Main_Day where f_id=" + _bt.Tag.ToString()+" and f_del=0 ";
                read_s.Close();
                comm_s = new SqlCommand(strSQL, conn_s);
                read_s = comm_s.ExecuteReader();
                while (read_s.Read())
                {
                    _bt.Text = read_s["f_name"].ToString() + "\n" + read_s["f_stime"].ToString();
                    _bt.BackColor = Color.FromName(read_s["f_color"].ToString());
                    _bt.Tag = read_s["f_id"].ToString();
                }
                read_s.Close();
                conn_s.Close();
                conn_s.Dispose();
                if (cb_Refresh.Checked)
                {
                    timer1.Enabled = true;
                }
            }
            else
            {
                if (cb_Refresh.Checked)
                {
                    timer1.Enabled = true;
                }
            }
        }
        #endregion
        #region//--筛选事件--//
        private void rb_WorkPlace_CheckedChanged(object sender, EventArgs e)
        {
            Init_QueryList();
        }
        private void Init_QueryList()
        {
            flp_Query.Controls.Clear();
            string strSQL = "";
            bool _bHandle = false;
            if (rb_WorkPlace.Checked)
            {
                strSQL = "select f_id,f_name from TS_S_B_Station_WorkPlace where f_del=0 and f_deep=1 and f_type=3 and f_level=',0,' and f_id not in (55,69) order by f_name";
                _bHandle = true;
            }
            if (rb_TrainType.Checked)
            {
                strSQL = "select * from TS_S_T_P_TrainType where f_del=0 and f_tcid=0 order by f_name";
                _bHandle = true;
            }
            if (rb_TrainNumber.Checked)
            {
                _bHandle = true;
            }
            if (_bHandle)
            {
                if (rb_TrainNumber.Checked)
                {
                    TextBox _tb = new TextBox();
                    _tb.Name = "tb_Name";
                    _tb.Width = 120;
                    _tb.MaxLength = 4;
                    _tb.TextChanged += new EventHandler(_tb_X_TextChanged);
                    flp_Query.Controls.Add(_tb);
                    Button _bt = new Button();
                    _bt.Name = "bt_Query";
                    _bt.Text = "查询编号";
                    _bt.Width = 120;
                    _bt.Height = 42;
                    _bt.Enabled = false;
                    _bt.Click += new EventHandler(_bt_X_Click);
                    flp_Query.Controls.Add(_bt);
                }
                else
                {
                    SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                    conn.Open();
                    SqlCommand comm = new SqlCommand(strSQL, conn);
                    SqlDataReader read = comm.ExecuteReader();
                    while (read.Read())
                    {
                        CheckBox _cb = new CheckBox();
                        _cb.Name = "cb_Q_" + read["f_id"].ToString();
                        _cb.Text = read["f_name"].ToString();
                        _cb.Width = 120;
                        _cb.Padding = new System.Windows.Forms.Padding(0);
                        _cb.Tag = read["f_id"].ToString();
                        _cb.Margin = new Padding(0);
                        _cb.CheckedChanged += new EventHandler(cb_Q_X_CheckedChanged);
                        flp_Query.Controls.Add(_cb);
                    }
                    read.Close();
                    conn.Close();
                    conn.Dispose();
                }
            }
            else
            {
                InitTrainInList();
            }
        }
        private void cb_Q_X_CheckedChanged(object sender, EventArgs e)
        {
            InitTrainInList();
        }
        private void _tb_X_TextChanged(object sender, EventArgs e)
        {
            Button _bt = (Button)flp_Query.Controls.Find("bt_Query", true)[0];
            TextBox _tb = (TextBox)sender;
            if (_tb.Text.Trim().Length > 0)
            {
                _bt.Enabled = true;
            }
            else
            {
                _bt.Enabled = false;
            }
        }

        private void _bt_X_Click(object sender, EventArgs e)
        {
            InitTrainInList();
        }
        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            //InitTrainInList();
            _ReloadViewTrainList();
        }

        private void cb_Refresh_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Refresh.Checked)
            {
                timer1.Enabled = true;
                cb_Refresh.Text = "自动刷新";
            }
            else
            {
                timer1.Enabled = false;
                cb_Refresh.Text = "手动刷新";
            }
        }

        private void rb_b_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_l.Checked)
            {
                _str_Font_Size = "0";
            }
            else
            {
                _str_Font_Size = "1";
            }
            InitTrainInList();
        }

        private void Locomotive_ElectricRepair_Work_WorkPub_Day_Pub_SizeChanged(object sender, EventArgs e)
        {
            foreach (Control _cl in flp_Main.Controls)
            {
                (_cl as Panel).Width = flp_Main.ClientSize.Width;
                foreach (Control _c_s in _cl.Controls)
                {
                    try
                    {
                        if (_str_Font_Size == "0")
                        {
                            (_c_s as Panel).Width = flp_Main.ClientSize.Width - 150;
                        }
                        else
                        {
                            (_c_s as Panel).Width = flp_Main.ClientSize.Width - 300;
                        }

                    }
                    catch
                    {

                    }
                }
            }
        }

        private void rb_Steps_All_CheckedChanged(object sender, EventArgs e)
        {
            InitTrainInList();
        }
    }
}